package com.ee.chat.api.config;

import com.ee.chat.api.listener.RedisListener;
import com.ee.chat.common.util.ConstantsUtil;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
@EnableCaching
public class RedisCacheConfig {
    //redis消息监听容器
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 可以添加多个 messageListener，配置不同的交换机
        container.addMessageListener(listenerAdapter, new PatternTopic(ConstantsUtil.USER_WS_OPERATION));
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(RedisListener receiver) {
        return new MessageListenerAdapter(receiver, "onMessage");
    }

}
